/*
 * Copyright (c) 2009 LastFM.Desktop Anton Pavlik.
 * All Rights Reserved.
 * 
 * You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, 
 * with or without modifications, and in Source or Object form, provided that You meet the following 
 * conditions:
 * 1. You must give any other recipients of the Work or Derivative Works a copy of this License; and
 *
 * 2. You must cause any modified files to carry prominent notices stating that You changed the files; 
 * and
 *
 * 3. You must retain, in the Source form of any Derivative Works that You distribute, all copyright, 
 * patent, trademark, and attribution notices from the Source form of the Work, excluding those notices 
 * that do not pertain to any part of the Derivative Works.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * You may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, Licensor provides the Work 
 * (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 * CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties 
 * or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. 
 * YOU ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE WORK
 * AND ASSUME ANY RISKS ASSOCIATED WITH YOUR EXERCISE OF PERMISSIONS UNDER THIS LICENSE. 
 */
package org.shake.lastfm.model;

import java.util.Calendar;

public class Track {

	private String location;
	private String info;
	private String annotation;
	private String image;
	private String creator;
	private String album;
	private String id;
	private String title;

	private TrackStatus status = TrackStatus.SCHEDULED;

    private long duration;

    private String imageUrl;
	
    private static final String TITLE_CREATOR_SEPARATOR = " - ";
	
	public TrackStatus getStatus()
    {
        return this.status;
    }

    public void setStatus(TrackStatus status)
    {
        this.status = status;
    }

	/**
	 * @return Returns the title.
	 */
	public String getTitle() {
		return this.title;
	}
	/**
	 * @param title The title to set.
	 */
	public void setTitle(String title) {
		this.title = title;
	}
	/**
	 * @return Returns the album.
	 */
	public String getAlbum() {
		return this.album;
	}
	/**
	 * @param album The album to set.
	 */
	public void setAlbum(String album) {
		this.album = album;
	}
	/**
	 * @return Returns the creator.
	 */
	public String getCreator() {
		return this.creator;
	}
	/**
	 * @param creator The creator to set.
	 */
	public void setCreator(String creator) {
		this.creator = creator;
	}
	/**
	 * @return Returns the identifier.
	 */
	public String getId() {
		return this.id;
	}
	/**
	 * @param identifier The identifier to set.
	 */
	public void setId(String identifier) {
		this.id = identifier;
	}
	/**
	 * @return Returns the annotation.
	 */
	public String getAnnotation() {
		return this.annotation;
	}
	/**
	 * @param annotation The annotation to set.
	 */
	public void setAnnotation(String annotation) {
		this.annotation = annotation;
	}
	/**
	 * @return Returns the image.
	 */
	public String getImage() {
		return this.image;
	}
	/**
	 * @param image The image to set.
	 */
	public void setImage(String image) {
		this.image = image;
	}
	/**
	 * @return Returns the location.
	 */
	public String getLocation() {
		return this.location;
	}
	/**
	 * @param location The location to set.
	 */
	public void setLocation(String location) {
		this.location = location;
	}
	/**
	 * @return Returns the info.
	 */
	public String getInfo() {
		return this.info;
	}
	/**
	 * @param info The info to set.
	 */
	public void setInfo(String info) {
		this.info = info;
	}
	
	public String getTrackName()
	{
	    StringBuffer sb = new StringBuffer();
	    sb.append(getTitle()).append(TITLE_CREATOR_SEPARATOR).append(getCreator());
	    return sb.toString();
	}
	
	public String toString()
	{
	    return getTrackName();
	}

    public void setDuration(long value)
    {
        this.duration = value;
    }
    
    public long getDuration()
    {
        return this.duration;
    }
    
    public String getImageUrl()
    {
        return this.imageUrl;
    }

    public void setImageUrl(String imageUrl)
    {
        this.imageUrl = imageUrl;
    }
    
    public String getFormattedDuration()
    {
        return getFormattedDuration(this.duration);
    }
    
    public static String getFormattedDuration(long duration)
    {
        Calendar instance = Calendar.getInstance();
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 0);
        instance.set(Calendar.MILLISECOND, 0);
        long d = duration;
        while (d > Integer.MAX_VALUE)
        {
            instance.add(Calendar.MILLISECOND, Integer.MAX_VALUE);
            d = d - Integer.MAX_VALUE;
        }
        instance.add(Calendar.MILLISECOND, (int) d);
        int minutes = instance.get(Calendar.MINUTE);
        int seconds = instance.get(Calendar.SECOND);

        return (minutes < 10 ? "0"+minutes : minutes) + ":" + (seconds < 10 ? "0"+seconds : seconds);
    }

    public boolean equals(Object anotherObj)
    {
        if (anotherObj == null || !anotherObj.getClass().equals(getClass()))
        {
            return false;
        }
        
        Track anotherTrack = (Track) anotherObj;
        
        return anotherTrack.getAlbum().equals(this.album) 
                && anotherTrack.getCreator().equals(this.creator)
                && anotherTrack.getTitle().equals(this.title);
    }

    @Override
    public int hashCode()
    {
        return this.album.hashCode() + this.creator.hashCode() + this.title.hashCode();
    }
}